var fs = require("fs");  

for(let i = 0; i < process.argv.length; ++i) {
    console.log(`index ${i} argument -> ${process.argv[i]}`);
}

const libname = process.argv[2];

console.log('libname: ', libname);

const js_file = libname + '/pkg/' + libname + '.js';

var content = fs.readFileSync(js_file, "utf-8");

var encoder_js = fs.readFileSync("./build_tools/encoding_utf8.min.js", "utf-8");


content = encoder_js + '\n' + 'import * as ' + libname + '_api from \'./wxapi.js\';' + '\n' + content;

// 替换 Webassembly
content = content.replaceAll('WebAssembly', 'WXWebAssembly');

// 删除 init函数中的两个行
content = content.replace(`input = new URL('` + libname + `_bg.wasm', import.meta.url);`, '');

content = content.replace(`input = fetch(input);`, '');

fs.writeFileSync(js_file, content);